package leetcode.bitoperation;

/**
 * @Description: https://leetcode.cn/problems/single-number/description/
 * @Author Ammar
 * @Create 2023/8/4 10:20
 */
public class _136_只出现一次的数字 {
    /**
     * a ^ b ^ a = (a ^ a) ^ b
     * 异或满足交换律和结合律
     * @param nums
     * @return
     */
    public int singleNumber(int[] nums) {
        int ans = nums[0];
        if (nums.length > 1) {
            for (int i = 1; i < nums.length; i++) {
                ans = ans ^ nums[i];
            }
        }
        return ans;
    }
}